<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Created by PhpStorm.
 * User: 265g
 * Date: 2015/2/25
 * Time: 11:28
 */

class MY_Model extends CI_Model{

    protected  $_table     = NULL;//用户表

    public      $error     = NULL;
    public      $userid    = NULL,
                 $username  = NULL;

    /**
    *   用户登陆判断
     */
    public function __construct(){
        $this->load->database();
    }

    /**
    *   数量统计
     */
    public function count($where){
        $this->_where($where);
        return $this->db->count_all_results($this->_table);
    }

    /**
    *   判断字段是否存在
     */
    public function exists($field, $value)
    {
        $this->db->where($field, $value);
        return $this->db->get($this->_table)->row_array() ? true : false;
    }


    /**
    *   数据插入操作
     */
    function insert($data = array())
    {
        $id = $this->db->insert($this->_table, $data);
        if ($id === false)
        {
            return false;
        }
        else
        {
            return $this->db->insert_id();
        }
    }

    /**
    *   数据的更新操作
     */
    function update($data = array(), $where = null)
    {
        $this->_where($where);
        $result = $this->db->update($this->_table,$data,$where);
        if ($result === FALSE)
        {
            return false;
        }
        else
        {
            return $result;
        }
    }
    /**
    *   增加自段值
     */
    public function set_inc($field, $where = null, $step = 1)
    {
        $this->_where($where);
        $this->db->set($field, $field.'+'.$step, false);
        return $this->db->update($this->_table);
    }

    /**
    *   较少字段值
     */
    public function set_dec($field, $where = null, $step = 1, $data = array())
    {
        $this->_where($where);
        $this->db->set($field, $field.'-'.$step, false);
        return $this->db->update($this->_table);
    }

    /**
    *   设置字段值
     */
    public function set_field($field, $value, $where = null)
    {
        return $this->update(array($field=>$value), $where);
    }


    /**
    *   删除数据
     */
    function delete($where = null)
    {
        $this->_where($where);
        $result = $this->db->delete($this->_table);
        if ($result === FALSE)
        {
            return false;
        }
        else
        {
            return $result;
        }
    }

    /**
    *   获取最大值
     */
    public function select_max($field = null){
        $this->db->select_max($field,'field_max');
        $return = $this->db->get($this->_table)->row_array();
        return $return['field_max'];
    }
    /**
     *   获取最小值
     */
    public function select_min($field = null){
        $this->db->select_max($field,'field_min');
        $return = $this->db->get($this->_table)->row_array();
        return $return['field_min'];
    }
    /**
     *   获取平均值
     */
    public function select_avg($field = null){
        $this->db->select_max($field,'field_avg');
        $return = $this->db->get($this->_table)->row_array();
        return $return['field_avg'];
    }
    /**
     *   获取总数
     */
    public function select_sum($field = null){
        $this->db->select_max($field,'field_sum');
        $return = $this->db->get($this->_table)->row_array();
        return $return['field_sum'];
    }

    /**
    *   单个字段查询
     */
    public function get_by_field($field = null, $value = null, $fields = "*"){
        if($field && $value){
            return $this->db->select($fields)->from($this->_table)->where($field, $value)->get()->row_array();
        }
        return false;
    }

    /**
    *   字段过滤
     */
    protected function filter_array($data, $keys)
    {
        foreach ($data as $field=>$v)
        {
            if (!in_array($field, $keys)) unset($data[$field]);
        }
        return $data;
    }

    /**
    *   返回错误信息
     */
    public function error(){
        return $this->error;
    }


    /**
    *   条件处理
     */
    private function _where($where)
    {
        if (isset($where['fields'])) {
            $this->db->select($where['fields']);
        } else if (isset($where['where'])) {
            $this->db->where($where['where']);
        } else if (isset($where['or_where'])) {
            $this->db->or_where($where['or_where']);
        } else if (isset($where['where_in'])) {
            $this->db->where_in($where['where_in']);
        } else if (isset($where['like'])) {
            $this->db->like($where['like']);
        } else if (isset($where['or_like'])) {
            $this->db->or_like($where['or_like']);
        } else if (isset($where['order'])) {
            $this->db->order_by($where['order']);
        } else if (isset($where['group'])) {
            $this->db->group_by($where['group']);
        } else if (isset($where['having'])) {
            $this->db->having($where['having']);
        } else {
            $this->db->where($where);
        }
    }
}